﻿using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace Albatross.Common.WCF
{
	public class DotNetDataContractSerializerAttribute : Attribute, IOperationBehavior
	{
		public void AddBindingParameters( OperationDescription description, BindingParameterCollection parameters )
		{
		}

		public void ApplyClientBehavior( OperationDescription description, System.ServiceModel.Dispatcher.ClientOperation proxy )
		{
			ReplaceDataContractSerializerOperationBehavior( description );
		}

		public void ApplyDispatchBehavior( OperationDescription description, System.ServiceModel.Dispatcher.DispatchOperation dispatch )
		{
			ReplaceDataContractSerializerOperationBehavior( description );
		}

		public void Validate( OperationDescription description )
		{
		}

		private static void ReplaceDataContractSerializerOperationBehavior( OperationDescription description )
		{
			DataContractSerializerOperationBehavior dcsOperationBehavior =
				description.Behaviors.Find<DataContractSerializerOperationBehavior>();

			if ( dcsOperationBehavior != null )
			{
				description.Behaviors.Remove( dcsOperationBehavior );
				description.Behaviors.Add( new DotNetDataContractOperationBehavior( description ) );
			}
		}                                
	}
}


